﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Bpt;
using ESOL.WinUI.UICommon;
using ESOL.Business.Mst;
using ESOL.Common;

namespace ESOL.WinUI.Bpt
{
    public partial class frmManageBlueprintChanges : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable

        private BptDrawBlueprintChangesEntity mSelectDrawBlueChanges;
        #endregion

        #region Constructor
        public frmManageBlueprintChanges()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions

        private void BingCombobox()
        {
            UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "BPTC", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "BPTC", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colChangeType, "BPTCT", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtChangeCode.Text) == false)
            {
                condition += " AND E.PRODUCT_SPEC_OUT LIKE '%" + txtChangeCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProAppCode.Text) == false)
            {
                condition += " AND B.PLAN_ORDER_CODE LIKE '%" + txtProAppCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtSponsorPerson.Text) == false)
            {
                condition += " AND A.SPONSOR_PERSON = '" + txtSponsorPerson.Text + "'";
            }
            if (string.IsNullOrEmpty(txtRecivePerson.Text) == false)
            {
                condition += " AND A.RECIVE_PERSON = '" + txtRecivePerson.Text + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND a.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpSponsorDateFrom.EditValue !=null)
            {
                condition += " AND A.SPONSOR_DATE >'" + dtpSponsorDateFrom.EditValue + "'";
            }
            if (dtpSponsorDateTo.EditValue != null)
            {
                condition += " AND A.SPONSOR_DATE <'" + dtpSponsorDateTo.EditValue + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.SPONSOR_DATE DESC ";

            DataTable dt = BptDrawBlueprintChangesBs.GetBptDrawPrintChangesByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void BingDetailGrid(string DrawBuleChangesID)
        {
            if (!DrawBuleChangesID.Equals(""))
            {
                DataTable dt = new DataTable();
                dt = BptDrawBlueprintChangesItemBs.GetBptDrawBlueprintChangesItemByID(DrawBuleChangesID);
                gvDetail.DataSource = dt;
            }
        }
        private void checkButtom(string status)
        {
            if (!status.Equals(""))
            {
                switch (status)
                {
                    case "BPTC01"://草稿
                        btnModify.Enabled = true;
                        btnApprove.Enabled = false;
                        btnDelete.Enabled = true;
                        btnSubmit.Enabled = true;
                        break;
                    case "BPTC02"://待审核
                        btnModify.Enabled = false;
                        btnApprove.Enabled = true;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = false;
                        break;
                    case "BPTC03"://已审核
                        btnModify.Enabled = false;
                        btnApprove.Enabled = false;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = false;
                        break;
                    case "BPTC04"://已拒绝
                        btnModify.Enabled = true;
                        btnApprove.Enabled = false;
                        btnDelete.Enabled = true;
                        btnSubmit.Enabled = true;
                        break;

                }
            }
        }
        private void SendMessage(string sMessageTitle, string sSender, string sMessageBody, string MessageLevel)
        {
            MstMessageEntity message = new MstMessageEntity();
            message.MessageId = Guid.NewGuid().ToString();
            message.MessateTitle = sMessageTitle;
            message.MessageSender = sSender;
            message.MessageBody = sMessageBody;
            message.MessageLevel = MessageLevel;
            message.FormName = "ESOL.WinUI.Bpt.frmManageDrawBlueprint";
            message.MessageDate = DateTime.Now;
            List<MstPersonEntity> toList = new List<MstPersonEntity>();
            //if (!string.IsNullOrEmpty(mSelectDrawBlue.ApprovedBy))
            //{
            //    MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectDrawBlue.ApprovedBy);
            //    toList.Add(person);
            //}
            //else
            //{
            //    toList = SecUserBs.GetPersonIdByFormAndObject("ESOL.WinUI.Bpt.frmDrawBlueprintManage", "审核", "brnApprove");
            //}
            MstMessageBs.SendMessage(message, toList);
        }
        #endregion

        #region Events
        private void frmBlueprintChangesManage_Load(object sender, EventArgs e)
        {
            //dtpSponsorDateFrom.EditValue = DateTime.Now.AddDays(-15);
            BingCombobox();
            BindDataGrid();

        }
        protected override void Click_View()
        {
            if (mSelectDrawBlueChanges == null)
            {
                MessageBox.Show("请选择一个制图任务！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainBlueprintChanges frm = new frmMaintainBlueprintChanges();
            frm.ThisActionType = ActionType.View;
            frm.SelectBptDrawBlueprintChanges = mSelectDrawBlueChanges;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_New()
        {

            frmMaintainBlueprintChanges frm = new frmMaintainBlueprintChanges();
            frm.ThisActionType = ActionType.New;
            frm.SelectBptDrawBlueprintChanges = new BptDrawBlueprintChangesEntity();
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectDrawBlueChanges == null)
            {
                MessageBox.Show("请选择一个图纸变更单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainBlueprintChanges frm = new frmMaintainBlueprintChanges();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectBptDrawBlueprintChanges = mSelectDrawBlueChanges;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Submit()
        {
            if (mSelectDrawBlueChanges == null)
            {
                MessageBox.Show("请选择一个图纸变更单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否提交选中图纸变更单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectDrawBlueChanges.Status = "BPTC02";
                mSelectDrawBlueChanges.LastmodifyBy = CommonParameter.CurrentUser.PersonId;
                mSelectDrawBlueChanges.LastmodifyDate = DateTime.Now;
                bool successful = BptDrawBlueprintChangesBs.UpdateBptDrawBlueprintChanges(mSelectDrawBlueChanges);
                if (successful)
                {
                    MessageBox.Show("提交成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectDrawBlueChanges == null)
            {
                MessageBox.Show("请选择一个图纸变更单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainBlueprintChanges frm = new frmMaintainBlueprintChanges();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectBptDrawBlueprintChanges = mSelectDrawBlueChanges;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mSelectDrawBlueChanges == null)
            {
                MessageBox.Show("请选择一个图纸变更单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中图纸变更单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                //mSelectDrawBlueChanges.Status = "DRAW06";
                mSelectDrawBlueChanges.LastmodifyBy = CommonParameter.CurrentUser.PersonId;
                mSelectDrawBlueChanges.LastmodifyDate = DateTime.Now;
                bool successful = BptDrawBlueprintChangesBs.DeleteBptDrawBlueprintChanges(mSelectDrawBlueChanges);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }


        #endregion

        //private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        //{
        //    DataRow dr = gridView1.GetDataRow(e.FocusedRowHandle);
        //    mSelectDrawBlueChanges = new BptDrawBlueprintChangesEntity(dr);
        //    BingDetailGrid(mSelectDrawBlueChanges.DrawChangesId);
        //    checkButtom(mSelectDrawBlueChanges.Status);

        //}

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectDrawBlueChanges = new BptDrawBlueprintChangesEntity(dr);
            BingDetailGrid(mSelectDrawBlueChanges.DrawChangesId);
            checkButtom(mSelectDrawBlueChanges.Status);

        }

    }
}